#!/usr/bin/python
# -*- coding: utf-8 -*-

DEVICE="eth1"
DOWN=50
UP=50
AVERAGE=15
import time

def getDev():
    x = open("/proc/net/dev",'r')
    for line in x.readlines():
        line = line.strip()
        devDelimiter = line.find(':')
        dev = line[:devDelimiter]
        line = line[devDelimiter+1:]
        if (dev == DEVICE):
            line = filter(lambda x: x, line.split(' '))
            down = int(line[0])
            up = int(line[8])
            return (down,up)

class Plugin():
    def name(self):
        return "bandwidth"

    def check(self):
        down,up = getDev()
        time.sleep(AVERAGE)
        newDown,newUp = getDev()
        downKb = ((newDown - down) / AVERAGE) / 1024
        upKb = ((newUp - up) / AVERAGE) / 1024
        if (downKb > DOWN):
            return "Downloading at %d" % (downKb)
        if (upKb > UP):
            return "Uploading at %d" % (upKb)
